package com.dyz.LeetCodeforSwordFingerOffer;

public class MergeTwoLists25 {
    //思路两个指针分别在两个链表中，在循环遍历
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1==null && l2==null){
            return l1;
        }
        if(l1==null){
            return l2;
        }
        if(l2==null){
            return l1;
        }

        ListNode dummy = new ListNode(0);
        ListNode cur =dummy;
        while (l1!=null && l2!=null){
            if(l1.val<=l2.val){
                cur.next = new ListNode(l1.val);
                l1 = l1.next;
            }else {
                cur.next = new ListNode(l2.val);
                l2 = l2.next;
            }
            cur = cur.next;
        }
        //遍历尾部剩余的
        while (l1!=null){
            cur.next = new ListNode(l1.val);
            l1 = l1.next;
            cur = cur.next;
        }
        while (l2!=null){
            cur.next = new ListNode(l2.val);
            l2 = l2.next;
            cur = cur.next;
        }
        return dummy.next;
    }
}
